import { Component, OnInit, ElementRef, Renderer2 } from '@angular/core';
import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
import { Badge } from '@ionic-native/badge/ngx';
import { BaiduPush, NotificationData, BaiduResponse } from '@ionic-native/baidu-push/ngx';
import { Router, NavigationExtras } from '@angular/router';
import VConsole from '../../../vconsole/vconsole.min.js';
import {TranslateService} from '@ngx-translate/core';
import { DefaultLanguageService } from '@services/default-language.service';
import { StatusBar } from '@ionic-native/status-bar/ngx';

@Component({
  selector: 'app-home',
  templateUrl: './home.page.html',
  styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {

  public appUserIdEmptyMsg:string;

  constructor(
    private el: ElementRef,
    private renderer2: Renderer2,
    private localNotifications: LocalNotifications,
    private badge: Badge,
    private baiduPush: BaiduPush,
    private router: Router,
    private translate: TranslateService,
    private defaultLanguageService:DefaultLanguageService,
    private statusBar: StatusBar
  ) {
    // this.notificationMsg();
    var vConsole = new VConsole();
    let defultLanguage = this.defaultLanguageService.getDefaultLanguage();
    this.translate.setDefaultLang(defultLanguage);
    this.translate.use(defultLanguage);
  }

  ngOnInit() {
    // this.notificationMsg();
  }

  toNotification(){
    this.router.navigate(['/notifications']);
  }

  badgeNumber: any = 0;
  setBadges() {
    this.badge.set(1).then(
      (data) => { console.info("set : " + data); this.badgeNumber + 1; },
      (err) => { console.info("err : " + err) }
    ).catch(
      (error) => {
        console.info("error + " + error);
      }
    );
  }
  getBadges() {
    this.badge.get().then(
      (num) => {
        this.badgeNumber = num;
        return this.badgeNumber;
      }
    );
    return this.badgeNumber;
  }
  increaseBadges() {
    this.badge.increase(1);
    this.badgeNumber = this.badgeNumber + 1;
  }

  singleNotification(notificationMsg:any) {
    this.localNotifications.schedule({
        id: 1,
        text: notificationMsg,//'Single ILocalNotification' + this.badgeNumber,
        sound: '',
        data: { secret: 1 },
        icon:'',
        title:''
      });
    this.historyList.push('Single ILocalNotification' + this.badgeNumber);
  }
  isAndroid = true;
  public badgeDisplay = 'none';

  toBadges(number) {
    let navigationExtras: NavigationExtras = {
      queryParams: {}
    };
    this.router.navigate(['/tabs/home/badges'], navigationExtras);
    sessionStorage.setItem('data', JSON.stringify({ number: number, list: this.historyList }));
  }

  public totalNum = 0;
  public historyList: any[] = [];
  addBadges() {
    this.singleNotification('');
    this.badgeDisplay = 'block';
    this.badgeNumber = this.badgeNumber + 1;
    this.totalNum += 1;
    if (this.badgeNumber > 9) {
      this.badgeNumber = '...';
      this.totalNum += 1;
    }
  }

  notificationMsg() {
    // var host = document.location.host;
    //判断当前浏览器是否支持WebSocket 
    // if ('WebSocket' in window) {
    //   console.log("浏览器支持Websocket")
    //   // let websocket = new WebSocket('ws://127.0.0.1:9446/oewebsocket/123');
    //   let websocket = new WebSocket('ws://192.168.0.107:9446/oewebsocket/123');
    //   //连接发生错误的回调方法 
    //   websocket.onerror = function (ev) {
    //     console.log(ev);
    //     console.log("WebSocket连接发生错误")
    //   };
    //   //连接成功建立的回调方法 
    //   websocket.onopen = function (evt:any) {
    //     console.log(evt);
    //     console.log('websocket response : '+evt);
    //   }

    //   var localNotifications = this.localNotifications;
    //   var historyList = this.historyList;
      
    //   //接收到消息的回调方法 
    //   websocket.onmessage = function (evt) {
    //     console.log("接收到消息的回调方法")
    //     console.log(evt.data);
        
    //     localNotifications.schedule({
    //       id: 1,
    //       text: evt.data,//'Single ILocalNotification' + this.badgeNumber,
    //       sound: '',
    //       data: { secret: 1 }
    //     });
    //     historyList.push('Single ILocalNotification' + evt.data);

    //     console.log("这是后台推送的消息：" + evt);
    //     // websocket.close();
    //   }
    //   //连接关闭的回调方法 
    //   websocket.onclose = function (ev) {
    //     console.log(ev);
    //     console.log("webSocket已关闭！")
    //   }
    // } else {
    //   console.log('Not support websocket')
    // }
  }

  ngAfterViewInit(): void {

  }

  notification() {
    // Schedule delayed notification
    this.localNotifications.schedule({
      text: 'Delayed ILocalNotification',
      trigger: { at: new Date(new Date().getTime() + 3600) },
      led: 'FF0000',
      sound: null
    });

    this.localNotifications.on('click').subscribe(result => {
      console.info('localNotifications : ' + result);
    });
  }
}
